package hsit;

import cn.hutool.poi.excel.ExcelWriter;

import java.io.File;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author huanggy
 * @date 2022/08/27
 **/
public class MethodInfoGenerate {

    public static void main(String[] args) {

        Class[] classes = getClassByPackage(MethodInfoGenerate.class.getPackage().getName());
        Map<String, List<String>> interfaceMethodMap = new HashMap<>();
        for (Class aClass : classes) {
//            ApplicationService annotation = (ApplicationService) aClass.getAnnotation(ApplicationService.class);
//            if(annotation == null){
//                System.out.println(aClass.getName());
//                continue;
//            }
//
//            String name = annotation.name();
//
//            for (Method method : aClass.getMethods()) {
//                com.alibaba.bizworks.core.specification.Method methodAnnotation = method.getAnnotation(com.alibaba.bizworks.core.specification.Method.class);
//               if(methodAnnotation == null){
//                   System.out.println(method.getName());
//                   continue;
//               }
//                String methodName = methodAnnotation.name();
//                PostMapping postMapping = method.getAnnotation(PostMapping.class);
//                String path = postMapping.path()[0];
//
//                String value = String.join("!!!", methodName, path);
//                if(interfaceMethodMap.containsKey(name)){
//                    interfaceMethodMap.get(name).add(value);
//                }else{
//                    List<String> list =new LinkedList<>();
//                    list.add(value);
//                    interfaceMethodMap.put(name, list);
//                }
//            }
        }

        ExcelWriter excelWriter = new ExcelWriter("D:\\Application\\kc.xlsx");

        List<List<Object>> data = new LinkedList<>();
        interfaceMethodMap.keySet().forEach(key->{

            for (String s : interfaceMethodMap.get(key)) {
                List<Object> content = new LinkedList<>();
                content.add(key);
                String[] split = s.split("!!!");
                content.add(split[0]);
                content.add(split[1]);
                data.add(content);
            }

        });

        excelWriter.write(data);
        excelWriter.close();

    }

    public static Class[] getClassByPackage(String packageName) {
        try {
            Enumeration<URL> resources = MethodInfoGenerate.class.getClassLoader().getResources(packageName.replaceAll("\\.", "/"));
            while (resources.hasMoreElements()) {
                URL url = resources.nextElement();
                String[] file = new File(url.getFile()).list();
                List<String> fileList = Arrays.stream(file).filter(fileUrl -> fileUrl.endsWith(".class") && !fileUrl.contains("hsit.CountMain.class")).collect(Collectors.toList());
                Class[] classList = new Class[fileList.size()];

                for (int i = 0; i < fileList.size(); i++) {
                    classList[i] = Class.forName(packageName + "." + fileList.get(i).replaceAll("\\.class", ""));
                }
                return classList;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
